package com.roc.util;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.Hashtable;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 身份证校验
 */
public class CheckIdCard {

        public static boolean iDCardValidate(String idCardNo) throws Exception {

            String[] ValCodeArr = {"1", "0", "x", "9", "8", "7", "6", "5", "4", "3", "2" };
            String[] Wi = {"7", "9", "10", "5", "8", "4", "2", "1", "6", "3", "7", "9", "10", "5", "8", "4", "2" };
            String Ai = "";

            try {
				
			} catch (Exception e) {
				// TODO: handle exception
			}
            if (idCardNo.length() != 15 && idCardNo.length() != 18) {
                return false;
            }
            if (idCardNo.length() == 18) {
                Ai = idCardNo.substring(0, 17);
            } else if (idCardNo.length() == 15) {
                Ai = idCardNo.substring(0, 6) + "19" + idCardNo.substring(6, 15);
            }
            if (isNumeric(Ai) == false) {
                return false;
            }
            String strYear = Ai.substring(6, 10);
            String strMonth = Ai.substring(10, 12);
            String strDay = Ai.substring(12, 14);
            if (isDate(strYear + "-" + strMonth + "-" + strDay) == false) {
                return false;
            }
            GregorianCalendar gc = new GregorianCalendar();
            SimpleDateFormat s = new SimpleDateFormat("yyyy-MM-dd");
            try {
                if ((gc.get(Calendar.YEAR) - Integer.parseInt(strYear)) > 150
                        || (gc.getTime().getTime() - s.parse(
                        strYear + "-" + strMonth + "-" + strDay).getTime()) < 0) {
                    return false;
                }
            } catch (NumberFormatException e) {
                e.printStackTrace();
            } catch (java.text.ParseException e) {
                e.printStackTrace();
            }
            if (Integer.parseInt(strMonth) > 12 || Integer.parseInt(strMonth) == 0) {
                return false;
            }
            if (Integer.parseInt(strDay) > 31 || Integer.parseInt(strDay) == 0) {
                return false;
            }
            Hashtable h = GetAreaCode();
            if (h.get(Ai.substring(0, 2)) == null) {
                return false;
            }
            int totalmulAiWi = 0;
            for (int i = 0; i < 17; i++) {
            	totalmulAiWi = totalmulAiWi
                        + Integer.parseInt(String.valueOf(Ai.charAt(i))) * Integer.parseInt(Wi[i]);
            }
            int modValue = totalmulAiWi % 11;
            String strVerifyCode = ValCodeArr[modValue];
            Ai = Ai + strVerifyCode;
            if (idCardNo.length() == 18) {
                if (Ai.toUpperCase().equals(idCardNo.toUpperCase()) == false) {
                    return false;
                }
            } else {
                return true;
            }
            return true;
        }
        @SuppressWarnings("unchecked")
        static Hashtable GetAreaCode() {
            Hashtable hashtable = new Hashtable();
            hashtable.put("11", "");
            hashtable.put("12", "");
            hashtable.put("13", "");
            hashtable.put("14", "");
            hashtable.put("15", "");
            hashtable.put("21", "");
            hashtable.put("22", "");
            hashtable.put("23", "");
            hashtable.put("31", "");
            hashtable.put("32", "");
            hashtable.put("33", "");
            hashtable.put("34", "");
            hashtable.put("35", "");
            hashtable.put("36", "");
            hashtable.put("37", "");
            hashtable.put("41", "");
            hashtable.put("42", "");
            hashtable.put("43", "");
            hashtable.put("44", "");
            hashtable.put("45", "");
            hashtable.put("46", "");
            hashtable.put("50", "");
            hashtable.put("51", "");
            hashtable.put("52", "");
            hashtable.put("53", "");
            hashtable.put("54", "");
            hashtable.put("61", "");
            hashtable.put("62", "");
            hashtable.put("63", "");
            hashtable.put("64", "");
            hashtable.put("65", "");
            hashtable.put("71", "");
            hashtable.put("81", "");
            hashtable.put("82", "");
            hashtable.put("91", "");
            return hashtable;
        }

        static boolean isNumeric(String str) {
            Pattern pattern = Pattern.compile("[0-9]*");
            Matcher isNum = pattern.matcher(str);
            if (isNum.matches()) {
                return true;
            } else {
                return false;
            }
        }

        static boolean isDate(String strDate) {
            Pattern pattern = Pattern
                    .compile("^((\\d{2}(([02468][048])|([13579][26]))[\\-\\/\\s]?((((0?[13578])|(1[02]))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(30)))|(0?2[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])))))|(\\d{2}(([02468][1235679])|([13579][01345789]))[\\-\\/\\s]?((((0?[13578])|(1[02]))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(30)))|(0?2[\\-\\/\\s]?((0?[1-9])|(1[0-9])|(2[0-8]))))))(\\s(((0?[0-9])|([1-2][0-3]))\\:([0-5]?[0-9])((\\s)|(\\:([0-5]?[0-9])))))?");
            Matcher m = pattern.matcher(strDate);
            if (m.matches()) {
                return true;
            } else {
                return false;
            }
        }

//        /**
//         * @param args
//         * @throws ParseException
//         */
//    @SuppressWarnings("static-access")
//    public static void main(String[] args){
//        // String IDCardNum="210102820826411";
//        // String IDCardNum="210102198208264114";
//        String IDCardNum = "441700197505084626";
//
//        try {
//			System.out.println(IDCardValidate(IDCardNum));
////        	System.out.println(new SimpleDateFormat("yyyyMMdd").format(new Date()));
//		} catch (Exception e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
//    }



}
